﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class PictureRepository : Data.Repository.IPictureRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region IPictureRepository Members

		public int CreateNewPicture(Data.Entity.Picture theNewPicture)
		{
			if(theNewPicture == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_Picture_Create(theNewPicture.PictureBinary, theNewPicture.Extension, theNewPicture.IsNew);
			}

			return result;
		}

		public IList<Data.Entity.Picture> GetPicturesByProduct(int productID)
		{
			if(productID <= 0)
			{
				return null;
			}

			List<ECommerce_GetPictures_By_ProductResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetPictures_By_Product(productID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Picture>();

				foreach(var d in lstData)
				{
					var theEntity = new Data.Entity.Picture()
					{
						Extension = d.Extension,
						IsNew = d.IsNew,
						PictureBinary = d.PictureBinary,
						PictureID = d.PictureID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int UpdatePicture(Data.Entity.Picture thePicture)
		{
			if(thePicture == null || thePicture.PictureID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbPicture = (from p in _dbContext.Pictures
								 where p.PictureID == thePicture.PictureID
								 select p).FirstOrDefault();

				if(dbPicture == null)
				{
					result = 0;
				}
				else
				{
					dbPicture.Extension = thePicture.Extension;
					dbPicture.IsNew = thePicture.IsNew;
					dbPicture.PictureBinary = thePicture.PictureBinary;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		public Data.Entity.Picture GetPicture(int id)
		{
			if(id <= 0)
			{
				return null;
			}

			List<ECommerce_GetPicture_By_PictureIDResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetPicture_By_PictureID(id);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var p = lstData[0];

				Data.Entity.Picture result;

				result = new Data.Entity.Picture()
				{
					Extension = p.Extension,
					IsNew = p.IsNew,
					PictureBinary = p.PictureBinary,
					PictureID = p.PictureID
				};

				return result;
			}

			return null;
		}

		public IList<Data.Entity.Picture> GetPicturesByProduct_WithoutBinary(int productid)
		{
			if(productid <= 0)
			{
				return null;
			}

			List<ECommerce_GetPictures_By_ProductResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetPictures_By_Product(productid);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.Picture>();

				foreach(var d in lstData)
				{
					var theEntity = new Data.Entity.Picture()
					{
						Extension = d.Extension,
						IsNew = d.IsNew,
						PictureID = d.PictureID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public Data.Entity.Picture GetTheProductDefaultPicture(int theProductID)
		{
			if(theProductID <= 0)
			{
				return null;
			}

			List<ECommerce_GetTheProductDefaultPictureResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{

				var data = _dbContext.ECommerce_GetTheProductDefaultPicture(theProductID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var p = lstData[0];

				Data.Entity.Picture result;

				result = new Data.Entity.Picture()
				{
					Extension = p.Extension,
					IsNew = p.IsNew,
					PictureBinary = p.PictureBinary,
					PictureID = p.PictureID
				};

				return result;
			}

			return null;
		}

		#endregion
	}
}
